#include <iostream>
#include <vector>

using namespace std;

void print(const vector<int> &vec)
{
    cout<<"size:"<<vec.size()<<endl;
    for(auto ite = vec.begin(); ite != vec.end(); ite++)
    {
        cout<<*ite<<" ";
    }
    cout<<endl;
}

int main()
{
    #if 0
    vector<int> vec = {1, 2, 3, 4, 5};
    cout<<"size:"<<vec.size()<<endl;
    for(int i= 0; i < vec.size(); i++)
    {
        cout<<vec[i]<<endl;
    }

    #if 0
    vector<int>::iterator ite = vec.begin();//迭代器
    #else
    //初始迭代器 [1,6)
    auto ite = vec.begin();//自动推导迭代器的类型
    cout<<*ite<<endl;
    #endif
    auto ite_end = vec.end();
    cout<<*(ite_end - 1)<<endl;
    #endif

    vector<int> vec;
    //尾插法
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);

    vec.pop_back();
    print(vec);

    vec.insert(vec.begin() + 1, 99);//插入
    print(vec);
    vec.erase(vec.begin() + 1);//删除
    print(vec);

    //使用迭代器iterator访问值
    vector<int>::iterator v = vec.begin();
    while(v != vec.end())
    {
        cout<<"value of v = "<<*v<<endl;
        v++;
    }
     // 使用auto和迭代器遍历vector
    for (auto i = vec.begin(); i != vec.end(); ++i) 
    {
        cout << *i << " ";
    }

    cout<<vec.capacity()<<endl;
    vec.shrink_to_fit();//减少内存
    cout<<vec.capacity()<<endl;
    

    return 0;
}